package com.zhengtoon.mctoon.conv.controller;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.bjtoon.framework.entity.ResponseResult;
import com.bjtoon.framework.sdk.toon.tooncode.ToonCodeInfo;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.toonCode.VisitorInfo;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.utils.ToonCodeAppUtils;
import com.zhengtoon.mctoon.conv.service.ISessionService;
import com.zhengtoon.mctoon.conv.vo.CityCodeVo;
import com.zhengtoon.mctoon.conv.vo.ToonCodeVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 会话管理
 */
@RestController
@RequestMapping("/session")
@Api(value = "session", description = "session")
public class SessionController {

    private static final Logger log = LoggerFactory.getLogger(SessionController.class);

    @Autowired
    ISessionService sessionService;

    /**
     * 获取用户的sesssionId（通过首页进入）
     * <p>
     * 144637
     *
     * @return ResponseResult
     */
    @ApiOperation(value = "getSessionIdByToonCode", httpMethod = "POST", notes = "getSessionIdByToonCode")
    @RequestMapping(value = "/getSessionIdByToonCode", method = RequestMethod.POST)
    public ResponseResult<Map<String, String>> getSessionIdByToonCode(
            @ApiParam(required = true, name = "toonCode", value = "toonCode参数") @RequestBody(required = true) ToonCodeVo toonCodeVo) {
        try {
            // 1. 参数校验
            if (StringUtils.isBlank(toonCodeVo.getToonCode())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }
            // 2. 解析code
            ToonCodeInfo toonCodeInfo = ToonCodeAppUtils.decryptToonCode(toonCodeVo.getToonCode().replaceAll(" ", "+"));

            if (toonCodeInfo == null) {
                return new ResponseResult<>(ConvPhotoExceptionCode.TOON_CODE_FAIL);
            }

            // 3. 判断toonCode过期
            if (ToonCodeAppUtils.isCodeExpired(toonCodeInfo)) {
                return new ResponseResult<>(ConvPhotoExceptionCode.TOON_CODE_EXPIRE);
            }

            // 4. 获取sessionId
            String sessionId = sessionService.querySessionIdByToonCode(toonCodeInfo);

            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("sessionId", sessionId);

            return ResponseResult.success(resultMap);
        } catch (BusinessException e) {
            return ResponseResult.fail(AppConstant.ERROR_CODE, e.getMessage());
        } catch (Exception e) {
            log.error("getSessionIdByToonCode()方法error", e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "getSessionIdByToonCode()方法error");
        }
    }

    /**
     * 获取用户的sesssionId（通过城市积分进入）
     * <p>
     * 144637
     *
     * @return ResponseResult
     */
    @ApiOperation(value = "getSessionIdByCityCode", httpMethod = "POST", notes = "getSessionIdByCityCode")
    @RequestMapping(value = "/getSessionIdByCityCode", method = RequestMethod.POST)
    public ResponseResult<Map<String, String>> getSessionIdByCityCode(
            @ApiParam(required = true, name = "cityCodeVo", value = "cityCodeVo参数") @RequestBody(required = true) CityCodeVo cityCodeVo) {
        try {
            // 1. 参数校验
            if (StringUtils.isBlank(cityCodeVo.getCityCode())) {
                return new ResponseResult<>(ConvPhotoExceptionCode.PARAM_IS_NULL);
            }

            // 2. 解析citycode
            String cityCode = ToonCodeAppUtils.decryptCityCode(cityCodeVo.getCityCode());

            ToonCodeInfo toonCodeInfo = new ToonCodeInfo();
            toonCodeInfo.setUserId(cityCode);
            // 4. 获取sessionId
            String sessionId = sessionService.querySessionIdByToonCode(toonCodeInfo);

            Map<String, String> resultMap = new HashMap<>();
            resultMap.put("sessionId", sessionId);

            return ResponseResult.success(resultMap);
        } catch (BusinessException e) {
            return ResponseResult.fail(AppConstant.ERROR_CODE, e.getMessage());
        } catch (Exception e) {
            log.error("getSessionIdByCityCode()方法error", e);
            return ResponseResult.fail(AppConstant.ERROR_CODE, "getSessionIdByCityCode()方法error");
        }
    }
}